﻿/*
  Project:       
  File:          AddRegisterHandler.ashx
  Description:   
 *               
 *               
 *               
  Author:        Lanny
  Date:          Aug,2014
  version:       2.0
  Modify:
 
                 
 Comments:
 * 1. 2014.07.17 对所有的字段增加了trim 操作

 
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using BLL;
using TaoBaoProject.Common;
using System.Web.SessionState;
using System.Threading;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace TaoBaoProject.Handler.RegisterHandler
{
  /// <summary>
  /// AddRegisterHandler 的摘要说明
  /// </summary>
  public class AddRegisterHandler : IHttpHandler, IRequiresSessionState
  {

    public string q1, q2;
    QQRelation qqrelationModel;
    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      try
      {
        string qq = context.Request.Params["QQ"] == null ? "" : context.Request.Params["QQ"].ToString().Trim();
        string wangwang = context.Request.Params["Wangwang"] == null ? "" : context.Request.Params["Wangwang"].ToString().Trim();
        string zhifubaoaccount = context.Request.Params["ZhifubaoAccount"] == null ? "" : context.Request.Params["ZhifubaoAccount"].ToString().Trim();
        string zhifubaoname = context.Request.Params["ZhifubaoName"] == null ? "" : context.Request.Params["ZhifubaoName"].ToString().Trim();
        string rebatezhifubao = context.Request.Params["RebateZhifubao"] == null ? "" : context.Request.Params["RebateZhifubao"].ToString();
        string rebatezhifubaoname = context.Request.Params["RebateZhifubaoName"] == null ? "" : context.Request.Params["RebateZhifubaoName"].ToString().Trim();
        string caifutongaccount = context.Request.Params["CaifutongAccount"] == null ? "" : context.Request.Params["CaifutongAccount"].ToString().Trim();
        string caifutongname = context.Request.Params["CaifutongName"] == null ? "" : context.Request.Params["CaifutongName"].ToString().Trim();
        string shopid = context.Request.Params["ShopID"] == null ? "" : context.Request.Params["ShopID"].ToString().Trim();
        string cash = context.Request.Params["Cash"] == null ? "" : context.Request.Params["Cash"].ToString().Trim();
        string cellphone = context.Request.Params["Cellphone"] == null ? "" : context.Request.Params["Cellphone"].ToString().Trim();
        string fixphone = context.Request.Params["Fixphone"] == null ? "" : context.Request.Params["Fixphone"].ToString().Trim();
        string receivename = context.Request.Params["ReceiveName"] == null ? "" : context.Request.Params["ReceiveName"].ToString().Trim();
        string receiveaddress = context.Request.Params["ReceiveAddress"] == null ? "" : context.Request.Params["ReceiveAddress"].ToString().Trim();
        string receivecellphone = context.Request.Params["ReceiveCellphone"] == null ? "" : context.Request.Params["ReceiveCellphone"].ToString().Trim();
        string receivefixphone = context.Request.Params["ReceiveFixphone"] == null ? "" : context.Request.Params["ReceiveFixphone"].ToString().Trim();
        string date = context.Request.Params["Date"] == null ? "" : context.Request.Params["Date"].ToString() + " " + DateTime.Now.ToLongTimeString();
        string province = context.Request.Params["hdprovince"] == null ? "" : context.Request.Params["hdprovince"].ToString().Trim();
        string city = context.Request.Params["hdcity"] == null ? "" : context.Request.Params["hdcity"].ToString().Trim();
        string district = context.Request.Params["hddistrict"] == null ? "" : context.Request.Params["hddistrict"].ToString().Trim();

        string beizhu = context.Request.Params["beizhu"] == null ? "" : context.Request.Params["beizhu"].ToString().Trim();
        string bankaccount = context.Request.Params["BankAccount"] == null ? "" : context.Request.Params["BankAccount"].ToString().Replace(" ", "").Trim();
        string bankname = context.Request.Params["BankName"] == null ? "" : context.Request.Params["BankName"].ToString().Trim();

        string banktype = context.Request.Params["BankType"] == null ? "" : context.Request.Params["BankType"].ToString().Trim();
        string source = context.Request.Params["Source"] == null ? "" : context.Request.Params["Source"].ToString().Trim();
        string qqrelation = context.Request.Params["QQRelation"] == null ? "" : context.Request.Params["QQRelation"].ToString().Trim();

        Register r = new Register();
        r.Qq = qq;
        r.Wangwang = wangwang;
        r.ZhifubaoAccount = zhifubaoaccount;
        r.ZhifubaoName = zhifubaoname;
        r.RebateZhifubao = rebatezhifubao;
        r.RebateZhifubaoName = rebatezhifubaoname;
        r.CaifutongAccount = caifutongaccount;
        r.CaifutongName = caifutongname;
        r.ShopId = Convert.ToInt32(shopid);
        r.Cash = cash;
        r.CellPhone = cellphone;
        r.FixPhone = fixphone;
        r.ReceiveName = receivename;
        r.ReceiveCellphone = receivecellphone;
        r.ReceiveFixphone = receivefixphone;
        r.ReceiveAddress = receiveaddress;
        r.Date = date;
        r.Province = province;
        r.City = city;
        r.District = district;
        r.Beizhu = beizhu;
        r.BankAccount = bankaccount;
        r.BankName = bankname;
        r.BankType = banktype;
        r.Source = source;
        r.QQRelation = qqrelation;
        r.CreatedBy = ContextWeb.CurrentUser.UserName;

        RegisterBLL.AddRegister(r);
        string returnValue = "{QQ:'" + r.Qq + "',Wangwang:'" + r.Wangwang + "',ZhifubaoAccount:'" + r.ZhifubaoAccount + "',ZhifubaoName:'" + r.ZhifubaoName + "',RebateZhifubao:'" + r.RebateZhifubao + "',RebateZhifubaoName:'" + r.RebateZhifubaoName + "',CaifutongAccount:'" + r.CaifutongAccount + "',CaifutongName:'" + r.CaifutongName + "',ShopID:'" + r.ShopId + "',Cash:'" + r.Cash + "',Cellphone:'" + r.CellPhone + "',Fixphone:'" + r.FixPhone + "',ReceiveName:'" + r.ReceiveName + "',ReceiveAddress:'" + r.ReceiveAddress + "',ReceiveCellphone:'" + r.ReceiveCellphone + "',ReceiveFixphone:'" + r.ReceiveFixphone + "',Date:'" + r.Date + "',QQRelation:'" + r.QQRelation + "'}";

        if (r.QQRelation != "" && r.QQRelation != null)
        {
          q1 = r.Qq;
          q2 = r.QQRelation;
          AddQQRelation();
        }
        context.Response.Write(returnValue);
        //string keyword = context.Request.Params["keyword"] == null ? "" : context.Request.Params["keyword"].ToString();
        //if (keyword.Split('|').Length > 2)
        //  GetAllRegisterBySearchPara(context);
        return;

      }
      catch (Exception ex)
      {
        context.Response.Write("{errorMsg:'新增登记失败'}");
      }
    }

    #region Not sure whether it's userful
    private bool checkFirstMode(string[] arr)
    {
      return arr[2].Contains("银行户名") && arr[3].Contains("银行账号") && arr[4].Contains("何种银行");
    }
    private bool checkSecondMode(string[] arr)
    {
      return arr[2].Contains("旺旺绑定支付宝") && arr[3].Contains("支付宝名字") && arr[4].Contains("返款财付通");
    }
    private void GetAllRegisterBySearchPara(HttpContext context)
    {
      string keyword = context.Request.Params["keyword"] == null ? "" : context.Request.Params["keyword"].ToString();
      keyword = keyword.Replace('|', '\n');

      string[] arrs = keyword.Split('\n');

      string isSearch = context.Request.Params["isSearch"] == null ? "" : context.Request.Params["isSearch"].ToString();

      SearchPara sp = new SearchPara();
      sp.haschecked = "true";
      sp.keywords = keyword;
      sp.datefrom = "1900-01-01 00:00:00";
      sp.dateto = "9999-01-01 00:00:00";
      sp.pricefrom = Int32.MinValue.ToString();
      sp.priceto = Int32.MaxValue.ToString();
      sp.limitdays = 0;
      sp.userName = "";
      sp.shopId = "";

      string path = HttpContext.Current.Server.MapPath(@"\") + "Config\\DefineSearchItems.xml";
      string allSerachItems = XmlOps.GetAllSearchItems(path);
      sp.searchScope = allSerachItems;

      int ItemTotalCount = 0;
      int pagesize = 10; ;
      int page = 1;
      //DataSet ds;
      DataTable dt = new DataTable();

      dt = RegisterBLL.GetAllRegisterBySearchPara(sp, pagesize, page, out ItemTotalCount);
      //***********Handler Shop Name*************
      for (int i = 0; i < dt.Rows.Count; i++)
      {
        if (dt.Rows[i]["ShopColor"].ToString().Trim() != "")
        {
          dt.Rows[i]["ShopName"] = dt.Rows[i]["ShopName"] + "," + dt.Rows[i]["ShopColor"];
        }
      }


      JsonSerializerSettings setting = new JsonSerializerSettings();
      IsoDateTimeConverter iso = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
      iso.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

      setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
      string result = JsonConvert.SerializeObject(new { total = ItemTotalCount, rows = dt }, Formatting.Indented, iso);
      context.Response.Write(result);
    }
    #endregion
    private void AddQQRelation()
    {
      QQRelationBLL qqrelationBLL = new QQRelationBLL();
      qqrelationModel = new QQRelation();
      var isExistQQ = qqrelationBLL.IsSameQQ(q1, q2);
      if (isExistQQ.Rows.Count == 0)
      {
        qqrelationModel.MainQQ = q1;
        qqrelationModel.QQ = q2;
        qqrelationModel.CreateTime = DateTime.Now.ToString();
        qqrelationBLL.AddQQRelation(qqrelationModel);
      }
    }

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}